
/**
 * 流程相关
 */

export default {
    provide() {
        return {
            $labProcess: this.labProcess,
            appInfo: this.appInfo
        }
    },
    inject: {
        /**
         * 框架方法
         * refreshTag 刷新当前页
         * closeTag   关闭当前页
         */
        $LabWeb: {
            from: '$LabWeb',
            default: function () {
                return undefined
            }
        },
        appInfo: {
            from: 'appInfo',
            default: function () {
                return { appId: '0' }
            }
        }
    },
    props: {
        /**
         * 当前组件
         */
        element: {
            type: Object,
            default: function () {
                return { elements: [], props: {} }
            }
        },
        /**
         * 当前业务对象
         */
        modelData: {
            type: Object,
            default: function () {
                return { }
            }
        }
    },
    data () {
        return {
            initLoading: false,
            visible: false,
            backVisible: false,
            args: { },
            allStep: [],
            activeStep: 0,
            labProcess: {
                info: { goActivity: '', executeIng: false },
                submit: this.processSubmit,
                back: this.processBack,
                refresh: this.processRefresh
            },
            assignInfo: { activityType: '', subValues: [] },
            executeResult: { code: 0, msg: '', data: undefined, id: undefined }
        }
    },
    mounted() {
        if (this.executeResult.id) {
            clearInterval(this.executeResult.id)
        }
    },
    destroyed() {
        if (this.executeResult.id) {
            clearInterval(this.executeResult.id)
        }
    },
    watch: {
        backVisible() {
            if (this.backVisible) {
                this.executeResult.code = 0
            }
        }
    },
    methods: {
        loadNode() {
            this.initLoading = true
            const { todoId, processDefId } = this.modelData
            if (!todoId && !processDefId) {
                this.initLoading = false
                return
            }
            this.$post('/process/node?appId=' + this.appInfo.appId, this.modelData)
                .then(res => {
                    this.$resultMsg(this, res, '', true)
                    if (res.code === 200) {
                        this.$Util.copyAttr(res.data,this.labProcess.info,null, this)
                        this.allStep.splice(0, this.allStep.length)
                        const { nexts, node, backs, activityView, appId } = this.labProcess.info
                        backs.forEach(item => {
                            this.allStep.push(item)
                        })
                        this.allStep.push(node)
                        nexts.forEach(item => {
                            this.allStep.push(item)
                        })
                        if (activityView) {
                            this.$set(this.element.props, 'callCmd', activityView)
                        } else if (node.activityView) {
                            this.$set(this.element.props, 'callCmd', node.activityView)
                        }
                        this.activeStep = backs.length
                        this.$set(this.labProcess.info, 'allStep', this.allStep)
                        this.$set(this.labProcess.info, 'goActivity', '')
                        this.$set(this.labProcess.info, 'executeIng', false)
                    }
                    this.initLoading = false
                })
        },
        exeProcess(type, args) {
            return new Promise(resolve => {
                this.labProcess.info.executeIng = true
                const { info } = this.labProcess
                args = args || this.args
                const body = args ? Object.assign({}, info, args) : info
                this.$post('/process/' + type + '?appId='+ this.appInfo.appId, body)
                    .then(res => {
                        const { code, data, msg } = res
                        this.executeResult.data = data
                        this.executeResult.msg = msg
                        this.executeResult.code = code
                        info.goActivity = ''
                        this.args = {}
                        this.backVisible = false
                        this.labProcess.info.executeIng = false
                        resolve(res)
                    })
            })
        },
        processSubmit(args) {
            return this.exeProcess('submit', args)
        },
        processBack(args) {
            return new Promise(resolve => {
                this.args = args || {}
                const { goActivity } = this.labProcess.info
                if (goActivity) {
                    this.exeProcess('back')
                        .then(res => {
                            resolve(res)
                        })
                } else {
                    this.backVisible = true
                    this.selectGoBack()
                        .then(res => {
                            resolve(res)
                        })
                }
            })
        },
        selectGoBack() {
          return new Promise(resolve => {
              this.executeResult.id = setInterval(() => {
                  if (this.executeResult.code !== 0) {
                      clearInterval(this.executeResult.id)
                      resolve(this.executeResult)
                  }
              }, 100)
          })
        },
        processRefresh() {
            return this.exeProcess('refresh')
        }
    }
}
